package com.wing.device.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.device.dao.DeviceTypeAttributeDao;
import com.wing.device.model.entity.DeviceTypeAttribute;
import com.wing.device.model.entity.DeviceTypeAttributeValue;
import com.wing.device.model.request.DeviceTypeAttributeForm;
import com.wing.device.model.request.DeviceTypeAttributeValueForm;
import com.wing.device.model.response.DeviceTypeAttributeVO;
import com.wing.product.model.entity.ProductAttribute;
import com.wing.product.model.entity.ProductAttributeValue;
import com.wing.product.model.request.ProductAttributeForm;
import com.wing.product.model.request.ProductAttributeValueForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 设备类型属性-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DeviceTypeAttributeServiceImpl extends ServiceImpl<DeviceTypeAttributeDao, DeviceTypeAttribute> implements DeviceTypeAttributeService {

    @Autowired
    private DeviceTypeAttributeValueService deviceTypeAttributeValueService;

    @Override
    public int add(DeviceTypeAttributeForm deviceTypeAttributeForm) {

        //校验

        DeviceTypeAttribute deviceTypeAttribute =new DeviceTypeAttribute();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(DeviceTypeAttribute.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(deviceTypeAttributeForm, deviceTypeAttribute, copyOptions);

        int insertCount=baseMapper.insert(deviceTypeAttribute);
        deviceTypeAttributeForm.setId(deviceTypeAttribute.getId());
        saveDetail(deviceTypeAttributeForm);
        return insertCount;
    }

    void saveDetail(DeviceTypeAttributeForm attributeForm){
        if(CollectionUtils.isNotEmpty(attributeForm.getDetailList())){
            for (DeviceTypeAttributeValueForm attributeValueForm:
                    attributeForm.getDetailList()) {
                DeviceTypeAttributeValue attributeValue=new DeviceTypeAttributeValue();
                BeanUtils.copyProperties(attributeValueForm,attributeValue);
                attributeValue.setDeviceAttributeId(attributeForm.getId());
                deviceTypeAttributeValueService.save(attributeValue);
            }
        }
    }

    @Override
    public List<DeviceTypeAttributeVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DeviceTypeAttributeVO> pageList4app(Page page, Map<String, Object> params) {
        List<DeviceTypeAttributeVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DeviceTypeAttributeVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int update(DeviceTypeAttributeForm attributeForm) {
        DeviceTypeAttribute attribute=new DeviceTypeAttribute();
        BeanUtils.copyProperties(attributeForm,attribute);
        int updateCount=baseMapper.updateById(attribute);
        deviceTypeAttributeValueService.remove(new QueryWrapper<DeviceTypeAttributeValue>().eq("device_attribute_id",attributeForm.getId()));
        saveDetail(attributeForm);
        return updateCount;
    }
}
